<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>AK JOB</title>
    <link rel="icon" type="image/x-icon" href="../../../layuiadmin/style/res/favicon.png"/>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="LAY-role-manage" lay-filter="LAY-role-manage"></table>
        </div>
    </div>
</div>

<script type="text/html" id="toolbarDemo">
</script>

<script type="text/html" id="statusTmp">
    {{#  if(d.status === 0){ }}
    <a class="layui-btn layui-bg-orange layui-btn-xs">已停止</a>
    {{#  } }}
    {{#  if(d.status === 1){ }}
    <a class="layui-btn layui-btn-xs">已启动</a>
    {{#  } }}
</script>

<script type="text/html" id="table-tool">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="execute">执行一次</a>
    {{#  if(d.status === 0){ }}
    <a class="layui-btn layui-btn-xs" lay-event="start">启动</a>
    {{#  } }}
    {{#  if(d.status === 1){ }}
    <a class="layui-btn layui-bg-orange layui-btn-xs" lay-event="close">停止</a>
    {{#  } }}
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-href="views/ops/jobTask/task_list.html#/jobId={{d.id}}" lay-text="{{d.name}}">执行日志</a>
</script>

<script type="text/html" id="onLineClientCountTmp">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="onLineCount">{{ d.onLineClientCount }}</a>
</script>

<script src="../../../layuiadmin/layui/layui.js"></script>
<script src="../../../layuiadmin/lib/juicer-min.js"></script>

<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'applicationJob','application', 'table', 'setter', 'auth', 'AppHelper', 'routerHelper', 'tableWraper'], function () {
        var $ = layui.$
            , form = layui.form
            , application = layui.application
            , applicationJob = layui.applicationJob
            , setter = layui.setter
            , table = layui.table
            , routerHelper = layui.routerHelper
            , tableWraper = layui.tableWraper
            , AppHelper = layui.AppHelper
            , util = layui.util
            , LayerPage = AppHelper.getLayerPage()
        ;
        var router = layui.router();
        var applicationId = router.search.applicationId;

        //初始化表格
        var currentTable = tableWraper.render({
            elem: "#LAY-role-manage",
            toolbar: '#toolbarDemo',
            url: setter.apiHost + "/admin/api/akApplicationJob/pageList/" + applicationId,
            cols: [[
                 {title: "任务名称", field: 'name', search: true}
                , {title: "任务状态", field: 'status', templet: "#statusTmp",width: 150}
                , {title: "cron表达式", field: 'cronStr'}
                , {title: "责任人", field: 'managerName'}
                , {title: "在线节点", field: 'onLineClientCount', templet: "#onLineClientCountTmp", maxWidth: 100}
                , {title: "创建时间", field: 'createTime', width: 200}
                , {fixed: 'right', title: "操作", align: 'center', toolbar: '#table-tool', minWidth: 300}
            ]]
        });

        table.on('toolbar(LAY-role-manage)', function (obj) {
            switch (obj.event) {
                case 'addApplicationJob':
                    var path = routerHelper.jump({
                        path: "job_form.html"
                        , query: {
                            action: "add"
                        }
                    })

                    LayerPage.open({
                        path: path
                        , title: "新建任务"
                        , iframeSubmitId: "#jobFormSubmit"
                    })
                    break;
            }
            ;
        });

        table.on("tool(LAY-role-manage)", function (obj) {
            var event = obj.event;
            var data = obj.data;
            var id = data.id;
            let that = this;
            switch (event) {
                case "onLineCount":
                    if (data.onLineClientCount <= 0) {
                        return
                    }
                    application.methods.clientList(data.applicationId).done(function (res) {
                        let msg = "";
                        $.each(res.data, function (index, item) {
                            if (index != 0) {
                                msg += "<br/>";
                            }
                            msg += item.clientUrl + ":" + item.clientPort;
                        });
                        if (msg == "") {
                            msg = "无可用节点";
                        }
                        layer.tips(msg, that);
                    });
                    break;
                case "edit":
                    var path = routerHelper.jump({
                        path: "job_form.html"
                        , query: {
                            action: "edit"
                            , id: id
                        }
                    })
                    LayerPage.open({
                        path: path
                        , title: "编辑任务"
                        , iframeSubmitId: "#applicationJobFormSubmit"
                    })
                    break;
                case "start":
                    layer.confirm("您确定要启动吗？", {icon: 0}, function (index) {
                        var loadIndex = layer.load(2);
                        applicationJob.methods.start(id).always(function () {
                            layer.close(loadIndex);
                        }).done(function (res) {
                            if (res.data) {
                                layer.msg("启动成功!", {icon: 1});
                            }
                            layer.close(index);
                            currentTable.reload();
                        });
                    })
                    break;
                case "close":
                    layer.confirm("您确定要停止吗？", {icon: 0}, function (index) {
                        var loadIndex = layer.load(2);
                        applicationJob.methods.close(id).always(function () {
                            layer.close(loadIndex);
                        }).done(function (res) {
                            if (res.data) {
                                layer.msg("停止成功!", {icon: 1});
                            }
                            layer.close(index);
                            currentTable.reload();
                        });
                    })
                    break;
                case "execute":
                    layer.prompt({title: '请输JSON格式自定义参数', formType: 2,
                        btn: ['执行', '关闭'],}, function(value, index, elem){
                        if(!isValidJson(value)){
                            layer.tips('不是有效的JSON格式', elem);
                            return ;
                        }
                        var loadIndex = layer.load(2);
                        applicationJob.methods.execute({id:id,paramsStr:value}).always(function () {
                            layer.close(loadIndex);
                        }).done(function (res) {
                            if (res.data) {
                                layer.msg("执行成功!", {icon: 1});
                            }
                            layer.close(index);
                        });
                    });
                    break;
            }
        })

        function isValidJson(value){
            if (!value && value.length == 0){
                return true;
            }
            try {
                let parsed  = JSON.parse(value);
                if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) {
                    return true;
                }
            } catch (e) {
                return false;
            }
            return false;
        }

    });
</script>
</body>
</html>
